package com.almeros.android.multitouch.library;

import ohos.multimodalinput.event.TouchEvent;

/**
 * @author Almer Thie (code.almeros.com)
 * Copyright (c) 2013, Almer Thie (code.almeros.com)
 */
public class MoveGestureDetector {

    private static final int INVALID_POINTER_INDEX = -1;
    private int mPointerIndex;
    private OnMoveGestureListener mListener;
    private float mX;
    private float mY;
    private float lastX;
    private float lastY;
    private float tempX;
    private float tempY;

    public MoveGestureDetector(OnMoveGestureListener listener) {
        mListener = listener;
        mPointerIndex = INVALID_POINTER_INDEX;
    }

    public float getX() {
        return tempX;
    }

    public float getY() {
        return tempY;
    }

    public boolean onTouchEvent(TouchEvent event) {
        switch (event.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                flag = true;
                mX = event.getPointerPosition(0).getX();
                mY = event.getPointerPosition(0).getY();
                lastX = event.getPointerPosition(0).getX();
                lastY = event.getPointerPosition(0).getY();
                mPointerIndex = event.getPointerId(0);
                // System.out.println("----------------------*********" + mY + "*******" + lastY);
                break;
            case TouchEvent.POINT_MOVE:
                if (mPointerIndex != INVALID_POINTER_INDEX && event.getPointerCount() != 0) {
                    // System.out.println("----------------------*********" + mY + "*******" + lastY);
                    mX = event.getPointerPosition(0).getX();
                    mY = event.getPointerPosition(0).getY();
                    if (Math.abs(mX - lastX) > 3 && Math.abs(mY - lastY) > 3) {
                        tempX = tempX + mX - lastX;
                        if (Math.abs(mY - lastY) < 100) {
                            tempY = tempY + mY - lastY;
                        }
                        if (mListener != null) {
                            mListener.onMove(this);
                        }
                        lastX = mX;
                        lastY = mY;
                        // System.out.println("----------------------************************************" + tempX + "*******" + tempY);
                    }
                }
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                mPointerIndex = INVALID_POINTER_INDEX;
                flag = false;
                break;
        }
        return true;
    }

    boolean flag = true;

    public static class SimpleOnScaleGestureListener implements OnMoveGestureListener {

        @Override
        public boolean onMove(MoveGestureDetector scaleDetector) {
            return false;
        }
    }

    public interface OnMoveGestureListener {

        boolean onMove(MoveGestureDetector scaleDetector);
    }
}
